package com.example.asus.homework11.activity;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.asus.homework11.R;
import com.example.asus.homework11.BaseListner;
import com.example.asus.homework11.BaseModel;
import com.example.asus.homework11.bean.ResultBean;
import com.example.asus.homework11.bean.UserBean;


public class UserDetailActivity extends AppCompatActivity {

    private ImageView topbar_back;
    private EditText et_username,et_password,et_phone,et_address;
    private Button btn_confirm;
    private SharedPreferences sharedPreferences;
    private int userid;
    private UserBean bean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_detail);
        initView();
        initData();
        initEvent();
    }

    private void initView(){
        topbar_back=findViewById(R.id.topbar_back);
        et_username=findViewById(R.id.et_username);
        et_password=findViewById(R.id.et_password);
        et_phone=findViewById(R.id.et_phone);
        et_address=findViewById(R.id.et_address);
        btn_confirm=findViewById(R.id.btn_confirm);
    }

    private void initData(){
        sharedPreferences = getSharedPreferences("User",MODE_PRIVATE);
        userid = Integer.parseInt(sharedPreferences.getString("userid","获取id失败"));
        BaseModel<UserBean> model=new BaseModel<>();
        model.callEnqueue(model.service.getUser(userid),listner);
    }

    private void initEvent(){
        topbar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseModel<ResultBean> model=new BaseModel<>();
                model.callEnqueue(model.service.updateUser(userid,et_username.getText().toString(),et_password.getText().toString(),et_phone.getText().toString(),et_address.getText().toString()),listner1);
            }
        });
    }

    BaseListner<UserBean> listner=new BaseListner<UserBean>() {
        @Override
        public void onResponse(UserBean userBean) {
            if(null!=userBean){
                bean=userBean;
                et_username.setText(userBean.getUsername());
                et_password.setText(userBean.getUserpass());
                et_phone.setText(userBean.getMobilenum()==null?"":userBean.getMobilenum());
                et_address.setText(userBean.getAddress()==null?"":userBean.getAddress());
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    BaseListner<ResultBean> listner1=new BaseListner<ResultBean>() {
        @Override
        public void onResponse(ResultBean resultBean) {
            if(null!=resultBean)
                if (resultBean.getSuccess().equals("1")){
                    Toast.makeText(UserDetailActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                    finish();
                }
                else
                    Toast.makeText(UserDetailActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(UserDetailActivity.this,msg,Toast.LENGTH_SHORT).show();
        }
    };

}
